package androidx.core.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.drawable.Drawable;
import android.icu.text.DecimalFormatSymbols;
import android.os.Build.VERSION;
import android.text.Editable;
import android.text.TextDirectionHeuristic;
import android.text.TextDirectionHeuristics;
import android.text.TextPaint;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.ActionMode;
import android.view.ActionMode.Callback;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.d;
import androidx.annotation.StyleRes;
import com.huawei.hms.framework.network.restclient.dnkeeper.DNKeeperConfig;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import o.gv;
import o.gv.a;
import o.hf;

public final class TextViewCompat {
    private static Field ˊ;
    private static boolean ˎ;
    private static Field ˏ;
    private static boolean ॱ;

    @RequiresApi(26)
    static class e implements Callback {
        private boolean ʻ = false;
        private final TextView ˊ;
        private Method ˋ;
        private Class ˎ;
        private final Callback ˏ;
        private boolean ॱ;

        e(Callback callback, TextView textView) {
            this.ˏ = callback;
            this.ˊ = textView;
        }

        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            return this.ˏ.onCreateActionMode(actionMode, menu);
        }

        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            ˎ(menu);
            return this.ˏ.onPrepareActionMode(actionMode, menu);
        }

        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            return this.ˏ.onActionItemClicked(actionMode, menuItem);
        }

        public void onDestroyActionMode(ActionMode actionMode) {
            this.ˏ.onDestroyActionMode(actionMode);
        }

        private void ˎ(Menu menu) {
            Method declaredMethod;
            int size;
            MenuItem item;
            List ˊ;
            int i;
            ResolveInfo resolveInfo;
            Context context = this.ˊ.getContext();
            PackageManager packageManager = context.getPackageManager();
            if (!this.ʻ) {
                this.ʻ = true;
                try {
                    this.ˎ = Class.forName("com.android.internal.view.menu.MenuBuilder");
                    this.ˋ = this.ˎ.getDeclaredMethod("removeItemAt", new Class[]{Integer.TYPE});
                    this.ॱ = true;
                } catch (ClassNotFoundException e) {
                    this.ˎ = null;
                    this.ˋ = null;
                    this.ॱ = false;
                    if (this.ॱ) {
                    }
                    declaredMethod = menu.getClass().getDeclaredMethod("removeItemAt", new Class[]{Integer.TYPE});
                    for (size = menu.size() - 1; size >= 0; size--) {
                        item = menu.getItem(size);
                        declaredMethod.invoke(menu, new Object[]{Integer.valueOf(size)});
                    }
                    ˊ = ˊ(context, packageManager);
                    for (i = 0; i < ˊ.size(); i++) {
                        resolveInfo = (ResolveInfo) ˊ.get(i);
                        menu.add(0, 0, i + 100, resolveInfo.loadLabel(packageManager)).setIntent(ॱ(resolveInfo, this.ˊ)).setShowAsAction(1);
                    }
                } catch (NoSuchMethodException e2) {
                    this.ˎ = null;
                    this.ˋ = null;
                    this.ॱ = false;
                    if (this.ॱ) {
                    }
                    declaredMethod = menu.getClass().getDeclaredMethod("removeItemAt", new Class[]{Integer.TYPE});
                    for (size = menu.size() - 1; size >= 0; size--) {
                        item = menu.getItem(size);
                        declaredMethod.invoke(menu, new Object[]{Integer.valueOf(size)});
                    }
                    ˊ = ˊ(context, packageManager);
                    for (i = 0; i < ˊ.size(); i++) {
                        resolveInfo = (ResolveInfo) ˊ.get(i);
                        menu.add(0, 0, i + 100, resolveInfo.loadLabel(packageManager)).setIntent(ॱ(resolveInfo, this.ˊ)).setShowAsAction(1);
                    }
                }
            }
            try {
                if (this.ॱ || !this.ˎ.isInstance(menu)) {
                    declaredMethod = menu.getClass().getDeclaredMethod("removeItemAt", new Class[]{Integer.TYPE});
                } else {
                    declaredMethod = this.ˋ;
                }
                for (size = menu.size() - 1; size >= 0; size--) {
                    item = menu.getItem(size);
                    if (item.getIntent() != null && "android.intent.action.PROCESS_TEXT".equals(item.getIntent().getAction())) {
                        declaredMethod.invoke(menu, new Object[]{Integer.valueOf(size)});
                    }
                }
                ˊ = ˊ(context, packageManager);
                for (i = 0; i < ˊ.size(); i++) {
                    resolveInfo = (ResolveInfo) ˊ.get(i);
                    menu.add(0, 0, i + 100, resolveInfo.loadLabel(packageManager)).setIntent(ॱ(resolveInfo, this.ˊ)).setShowAsAction(1);
                }
            } catch (NoSuchMethodException e3) {
            } catch (IllegalAccessException e4) {
            } catch (InvocationTargetException e5) {
            }
        }

        private List<ResolveInfo> ˊ(Context context, PackageManager packageManager) {
            List<ResolveInfo> arrayList = new ArrayList();
            if (!(context instanceof Activity)) {
                return arrayList;
            }
            for (ResolveInfo resolveInfo : packageManager.queryIntentActivities(ˎ(), 0)) {
                if (ˎ(resolveInfo, context)) {
                    arrayList.add(resolveInfo);
                }
            }
            return arrayList;
        }

        private boolean ˎ(ResolveInfo resolveInfo, Context context) {
            if (context.getPackageName().equals(resolveInfo.activityInfo.packageName)) {
                return true;
            }
            if (!resolveInfo.activityInfo.exported) {
                return false;
            }
            if (resolveInfo.activityInfo.permission == null || context.checkSelfPermission(resolveInfo.activityInfo.permission) == 0) {
                return true;
            }
            return false;
        }

        private Intent ॱ(ResolveInfo resolveInfo, TextView textView) {
            return ˎ().putExtra("android.intent.extra.PROCESS_TEXT_READONLY", !ˊ(textView)).setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
        }

        private boolean ˊ(TextView textView) {
            return (textView instanceof Editable) && textView.onCheckIsTextEditor() && textView.isEnabled();
        }

        private Intent ˎ() {
            return new Intent().setAction("android.intent.action.PROCESS_TEXT").setType(DNKeeperConfig.DNKEEPER_CONTENT_TYPE);
        }
    }

    private static Field ˋ(String str) {
        Field field = null;
        try {
            field = TextView.class.getDeclaredField(str);
            field.setAccessible(true);
            return field;
        } catch (NoSuchFieldException e) {
            Log.e("TextViewCompat", "Could not retrieve " + str + " field.");
            return field;
        }
    }

    private static int ॱ(Field field, TextView textView) {
        try {
            return field.getInt(textView);
        } catch (IllegalAccessException e) {
            Log.d("TextViewCompat", "Could not retrieve value of " + field.getName() + " field.");
            return -1;
        }
    }

    public static void ॱ(@NonNull TextView textView, @Nullable Drawable drawable, @Nullable Drawable drawable2, @Nullable Drawable drawable3, @Nullable Drawable drawable4) {
        if (VERSION.SDK_INT >= 18) {
            textView.setCompoundDrawablesRelative(drawable, drawable2, drawable3, drawable4);
        } else if (VERSION.SDK_INT >= 17) {
            Drawable drawable5;
            Object obj = textView.getLayoutDirection() == 1 ? 1 : null;
            if (obj != null) {
                drawable5 = drawable3;
            } else {
                drawable5 = drawable;
            }
            if (obj == null) {
                drawable = drawable3;
            }
            textView.setCompoundDrawables(drawable5, drawable2, drawable, drawable4);
        } else {
            textView.setCompoundDrawables(drawable, drawable2, drawable3, drawable4);
        }
    }

    public static void ˎ(@NonNull TextView textView, @Nullable Drawable drawable, @Nullable Drawable drawable2, @Nullable Drawable drawable3, @Nullable Drawable drawable4) {
        if (VERSION.SDK_INT >= 18) {
            textView.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, drawable2, drawable3, drawable4);
        } else if (VERSION.SDK_INT >= 17) {
            Drawable drawable5;
            Object obj = textView.getLayoutDirection() == 1 ? 1 : null;
            if (obj != null) {
                drawable5 = drawable3;
            } else {
                drawable5 = drawable;
            }
            if (obj == null) {
                drawable = drawable3;
            }
            textView.setCompoundDrawablesWithIntrinsicBounds(drawable5, drawable2, drawable, drawable4);
        } else {
            textView.setCompoundDrawablesWithIntrinsicBounds(drawable, drawable2, drawable3, drawable4);
        }
    }

    public static int ˊ(@NonNull TextView textView) {
        if (VERSION.SDK_INT >= 16) {
            return textView.getMaxLines();
        }
        if (!ˎ) {
            ˊ = ˋ("mMaxMode");
            ˎ = true;
        }
        if (ˊ != null && ॱ(ˊ, textView) == 1) {
            if (!ॱ) {
                ˏ = ˋ("mMaximum");
                ॱ = true;
            }
            if (ˏ != null) {
                return ॱ(ˏ, textView);
            }
        }
        return -1;
    }

    public static void ˎ(@NonNull TextView textView, @StyleRes int i) {
        if (VERSION.SDK_INT >= 23) {
            textView.setTextAppearance(i);
        } else {
            textView.setTextAppearance(textView.getContext(), i);
        }
    }

    @NonNull
    public static Drawable[] ˎ(@NonNull TextView textView) {
        int i = 1;
        if (VERSION.SDK_INT >= 18) {
            return textView.getCompoundDrawablesRelative();
        }
        if (VERSION.SDK_INT < 17) {
            return textView.getCompoundDrawables();
        }
        if (textView.getLayoutDirection() != 1) {
            i = 0;
        }
        Drawable[] compoundDrawables = textView.getCompoundDrawables();
        if (i != 0) {
            Drawable drawable = compoundDrawables[2];
            Drawable drawable2 = compoundDrawables[0];
            compoundDrawables[0] = drawable;
            compoundDrawables[2] = drawable2;
        }
        return compoundDrawables;
    }

    @NonNull
    @RestrictTo({d.ॱ})
    public static Callback ˏ(@NonNull TextView textView, @NonNull Callback callback) {
        return (VERSION.SDK_INT < 26 || VERSION.SDK_INT > 27 || (callback instanceof e)) ? callback : new e(callback, textView);
    }

    public static void ˋ(@NonNull TextView textView, @Px @IntRange(from = 0) int i) {
        hf.ˏ(i);
        if (VERSION.SDK_INT >= 28) {
            textView.setFirstBaselineToTopHeight(i);
            return;
        }
        int i2;
        FontMetricsInt fontMetricsInt = textView.getPaint().getFontMetricsInt();
        if (VERSION.SDK_INT < 16 || textView.getIncludeFontPadding()) {
            i2 = fontMetricsInt.top;
        } else {
            i2 = fontMetricsInt.ascent;
        }
        if (i > Math.abs(i2)) {
            textView.setPadding(textView.getPaddingLeft(), i - (-i2), textView.getPaddingRight(), textView.getPaddingBottom());
        }
    }

    public static void ॱ(@NonNull TextView textView, @Px @IntRange(from = 0) int i) {
        int i2;
        hf.ˏ(i);
        FontMetricsInt fontMetricsInt = textView.getPaint().getFontMetricsInt();
        if (VERSION.SDK_INT < 16 || textView.getIncludeFontPadding()) {
            i2 = fontMetricsInt.bottom;
        } else {
            i2 = fontMetricsInt.descent;
        }
        if (i > Math.abs(i2)) {
            textView.setPadding(textView.getPaddingLeft(), textView.getPaddingTop(), textView.getPaddingRight(), i - i2);
        }
    }

    public static int ॱ(@NonNull TextView textView) {
        return textView.getPaddingTop() - textView.getPaint().getFontMetricsInt().top;
    }

    public static int ˋ(@NonNull TextView textView) {
        return textView.getPaddingBottom() + textView.getPaint().getFontMetricsInt().bottom;
    }

    public static void ˏ(@NonNull TextView textView, @Px @IntRange(from = 0) int i) {
        hf.ˏ(i);
        int fontMetricsInt = textView.getPaint().getFontMetricsInt(null);
        if (i != fontMetricsInt) {
            textView.setLineSpacing((float) (i - fontMetricsInt), 1.0f);
        }
    }

    @NonNull
    public static a ˏ(@NonNull TextView textView) {
        if (VERSION.SDK_INT >= 28) {
            return new a(textView.getTextMetricsParams());
        }
        a.d dVar = new a.d(new TextPaint(textView.getPaint()));
        if (VERSION.SDK_INT >= 23) {
            dVar.ॱ(textView.getBreakStrategy());
            dVar.ˋ(textView.getHyphenationFrequency());
        }
        if (VERSION.SDK_INT >= 18) {
            dVar.ˊ(ᐝ(textView));
        }
        return dVar.ˏ();
    }

    public static void ˎ(@NonNull TextView textView, @NonNull a aVar) {
        if (VERSION.SDK_INT >= 18) {
            textView.setTextDirection(ॱ(aVar.ˏ()));
        }
        if (VERSION.SDK_INT < 23) {
            float textScaleX = aVar.ˎ().getTextScaleX();
            textView.getPaint().set(aVar.ˎ());
            if (textScaleX == textView.getTextScaleX()) {
                textView.setTextScaleX((textScaleX / 2.0f) + 1.0f);
            }
            textView.setTextScaleX(textScaleX);
            return;
        }
        textView.getPaint().set(aVar.ˎ());
        textView.setBreakStrategy(aVar.ॱ());
        textView.setHyphenationFrequency(aVar.ˊ());
    }

    public static void ˎ(@NonNull TextView textView, @NonNull gv gvVar) {
        if (ˏ(textView).ॱ(gvVar.ˊ())) {
            textView.setText(gvVar);
            return;
        }
        throw new IllegalArgumentException("Given text can not be applied to TextView.");
    }

    @RequiresApi(18)
    private static TextDirectionHeuristic ᐝ(@NonNull TextView textView) {
        int i = 1;
        if (textView.getTransformationMethod() instanceof PasswordTransformationMethod) {
            return TextDirectionHeuristics.LTR;
        }
        if (VERSION.SDK_INT < 28 || (textView.getInputType() & 15) != 3) {
            if (textView.getLayoutDirection() != 1) {
                i = 0;
            }
            switch (textView.getTextDirection()) {
                case 2:
                    return TextDirectionHeuristics.ANYRTL_LTR;
                case 3:
                    return TextDirectionHeuristics.LTR;
                case 4:
                    return TextDirectionHeuristics.RTL;
                case 5:
                    return TextDirectionHeuristics.LOCALE;
                case 6:
                    return TextDirectionHeuristics.FIRSTSTRONG_LTR;
                case 7:
                    return TextDirectionHeuristics.FIRSTSTRONG_RTL;
                default:
                    if (i != 0) {
                        return TextDirectionHeuristics.FIRSTSTRONG_RTL;
                    }
                    return TextDirectionHeuristics.FIRSTSTRONG_LTR;
            }
        }
        byte directionality = Character.getDirectionality(DecimalFormatSymbols.getInstance(textView.getTextLocale()).getDigitStrings()[0].codePointAt(0));
        if (directionality == (byte) 1 || directionality == (byte) 2) {
            return TextDirectionHeuristics.RTL;
        }
        return TextDirectionHeuristics.LTR;
    }

    @RequiresApi(18)
    private static int ॱ(@NonNull TextDirectionHeuristic textDirectionHeuristic) {
        if (textDirectionHeuristic == TextDirectionHeuristics.FIRSTSTRONG_RTL || textDirectionHeuristic == TextDirectionHeuristics.FIRSTSTRONG_LTR) {
            return 1;
        }
        if (textDirectionHeuristic == TextDirectionHeuristics.ANYRTL_LTR) {
            return 2;
        }
        if (textDirectionHeuristic == TextDirectionHeuristics.LTR) {
            return 3;
        }
        if (textDirectionHeuristic == TextDirectionHeuristics.RTL) {
            return 4;
        }
        if (textDirectionHeuristic == TextDirectionHeuristics.LOCALE) {
            return 5;
        }
        if (textDirectionHeuristic == TextDirectionHeuristics.FIRSTSTRONG_LTR) {
            return 6;
        }
        if (textDirectionHeuristic == TextDirectionHeuristics.FIRSTSTRONG_RTL) {
            return 7;
        }
        return 1;
    }
}
